#include "servermessagemanager.h"

ServerMessageManager::ServerMessageManager(QObject *parent, int port) :
    QObject(parent)
{
    serverConnection = new ServerConnection(this);
    serverConnection->setPort(port);
    serverConnection->run();

    connect(serverConnection, SIGNAL(signalConnectionError()),
            this, SLOT(slotConnectionError()));
    connect(serverConnection, SIGNAL(signalReceivedFromClient(int,QString)),
            this, SLOT(slotReceivedFromClient(int,QString)));
    connect(serverConnection, SIGNAL(signalReceivedFromPlayer(int,QString)),
            this, SLOT(slotReceivedFromPlayer(int,QString)));
    connect(serverConnection, SIGNAL(signalClientDisconnected(int)),
            this, SLOT(slotClientDisconnected(int)));
    connect(serverConnection, SIGNAL(signalPlayerDisconnected(int)),
            this, SLOT(slotPlayerDisconnected(int)));

}

ServerMessageManager::~ServerMessageManager()
{
    //send disconnect signals
    //break connections;
    delete serverConnection;
}

void ServerMessageManager::slotConnectionError()
{
    qDebug() << "ServerMessageManager slotConnectionError\n";
    emit signalConnectionError();
}

void ServerMessageManager::slotReceivedFromClient(int localId, QString str)
{

    qDebug() << "ServerMessageManager slotReceivedFromClient(localId:"
             << localId
             << "message"
             << str;
    parseToClientsCommand(localId, str);
}

void ServerMessageManager::slotReceivedFromPlayer(int playerId, QString str)
{
    qDebug() << "ServerMessageManager slotReceivedFromPlayer(playerId:"
             << playerId
             << "message"
             << str;
    parseToPlayersCommand(playerId, str);
}

void ServerMessageManager::slotSC_CONN_ACCEPTED(int localId, int playerId)
{
    serverConnection->addPlayer(localId, playerId);

    QString str = "SC_CONN_ACCEPTED"
            + delimetrs::delim
            + (QString("%1").arg(playerId))
            + delimetrs::eom;
    serverConnection->sendToPlayer(playerId, str);
}

void ServerMessageManager::slotSC_CONN_REJECTED(int localId, QString message)
{
    QString str = "SC_CONN_REJECTED" +
            delimetrs::delim
            + message
            + delimetrs::eom;
    serverConnection->sendToClient(localId, str);
}

void ServerMessageManager::slotSC_PLAYERLIST(QString str)
{
    qDebug()<<"slotSC_PLAYERLIST";
    QString strCommand = "SC_PLAYERLIST" +
            delimetrs::delim
            + str
            + delimetrs::eom;
    serverConnection->sendToAllSockets(strCommand);
}

void ServerMessageManager::slotSC_PLAYERLIST(int localId, QString str)
{
    QString strCommand = "SC_PLAYERLIST" +
            delimetrs::delim
            + str
            + delimetrs::eom;
    serverConnection->sendToClient(localId, strCommand);
}


const int ServerMessageManager::getPort()
{
    return port;
}

const QString ServerMessageManager::getIP()
{
    return serverConnection->getIP();
}

void ServerMessageManager::slotSC_GAME_START()
{
    QString str = "SC_GAME_START" + delimetrs::eom;
    serverConnection->gameStarts();
    serverConnection->sendToAllPlayers(str);
}

void ServerMessageManager::slotSC_GAME_END(int playerId)
{
    QString str = "SC_GAME_END"
            + delimetrs::delim
            + (QString("%1").arg(playerId))
            + delimetrs::eom;
    serverConnection->sendToAllPlayers(str);
}

void ServerMessageManager::slotSC_SEND_SNAPSHOT(QString snapshot)
{
    QString str = "SC_SEND_SNAPSHOT"
            + delimetrs::delim
            + snapshot
            + delimetrs::eom;
    serverConnection->sendToAllPlayers(str);
}

void ServerMessageManager::slotSC_SEND_MAP(int width, int height, const QString &map, int health, int motion_speed, int bullet_speed, int fire_rate)
{
    QString str = "SC_SEND_MAP"
            + delimetrs::delim
            + (QString("%1").arg(width))
            + delimetrs::delim
            + map
            + delimetrs::delim
            + (QString("%1").arg(health))
            + delimetrs::delim
            + (QString("%1").arg(motion_speed))
            + delimetrs::delim
            + (QString("%1").arg(bullet_speed))
            + delimetrs::delim
            + (QString("%1").arg(fire_rate))
            + delimetrs::eom;
    qDebug() << "ServerMessageManager::slotSC_SEND_MAP = " << str;
    serverConnection->sendToAllPlayers(str);
}

void ServerMessageManager::slotSC_MOVE(int playerId)
{
    qDebug() << "ServerMessageManager::slotSC_MOVE " << playerId;
    QString str = "SC_MOVE"
            + delimetrs::delim
            + (QString("%1").arg(playerId))
            + delimetrs::eom;
    serverConnection->sendToAllPlayers(str);
}

void ServerMessageManager::slotSC_FIRE(int playerId)
{
    QString str = "SC_FIRE"
            + delimetrs::delim
            + (QString("%1").arg(playerId))
            + delimetrs::eom;
    serverConnection->sendToAllPlayers(str);
}

void ServerMessageManager::slotSC_TURN(int playerId, char direction)
{
    QString str = "SC_TURN"
            + delimetrs::delim
            + (QString("%1").arg(playerId))
            + delimetrs::delim
            + direction
            + delimetrs::eom;
    serverConnection->sendToAllPlayers(str);
}

void ServerMessageManager::slotSC_STOP_MOVE(int playerId)
{
    QString str = "SC_STOP_MOVE"
            + delimetrs::delim
            + (QString("%1").arg(playerId))
            + delimetrs::eom;
    serverConnection->sendToAllPlayers(str);
}

void ServerMessageManager::slotSC_STOP_FIRE(int playerId)
{
    QString str = "SC_STOP_FIRE"
            + delimetrs::delim
            + (QString("%1").arg(playerId))
            + delimetrs::eom;
    serverConnection->sendToAllPlayers(str);
}

void ServerMessageManager::slotClientDisconnected(int localId)
{
    emit signalClientDisconnected(localId);
}

void ServerMessageManager::slotPlayerDisconnected(int playerId)
{
    emit signalPlayerDisconnected(playerId);
}

void ServerMessageManager::parseToPlayersCommand(int playerId, QString str)
{
    QString strWithoutEOM = str.left(str.length()-2);
    QStringList listOfElements = strWithoutEOM.split(delimetrs::delim);

    qDebug() << "ServerMessageManager::parsePlayersToCommand(int playerId, QString str)";

    if (listOfElements.at(0) == "CS_DISCONNECT")
    {
        emit signalDISCONNECT(playerId);
        return;
    }
    if (listOfElements.at(0) == "CS_FIRE")
    {
        emit signalFIRE(playerId);
        return;
    }
    if (listOfElements.at(0) == "CS_MOVE")
    {
        emit signalMOVE(playerId);
        return;
    }

    if (listOfElements.at(0) == "CS_TURN")
    {
       emit signalTURN(playerId, listOfElements.at(1).at(0).toAscii());
       return;
    }

    if (listOfElements.at(0) == "CS_STOP_MOVE")
    {
        emit signalSTOP_MOVE(playerId);
        return;
    }

    if (listOfElements.at(0) == "CS_STOP_FIRE")
    {
        emit signalSTOP_FIRE(playerId);
        return;
    }

    emit signalUNKNOWN_COMMAND(str);

}

void ServerMessageManager::parseToClientsCommand(int localId, QString str)
{
    QString strWithoutEOM = str.left(str.length()-2);
    QStringList listOfElements = strWithoutEOM.split(delimetrs::delim);

    qDebug() << "ServerMessageManager::parseClientsToCommand(int playerId, QString str)";
    if (listOfElements.at(0) == "CS_SEND_NICK")
    {
        qDebug() << "CS_SEND_NICK";
        qDebug() << localId;
        qDebug() << "nick=";
        qDebug() << listOfElements.at(1);
        emit signalSEND_NICK(localId, listOfElements.at(1));
        return;
    }
    emit signalUNKNOWN_COMMAND(str);
}
